/*
 * ConfirmarCarrito.java
 *
 * Created on 10 de abril de 2007, 14:22
 */

package despachador.pagina;

import excepciones.AvwException;
import gestores.pagina.GestorCarrito;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author Administrador
 * @version
 */
public class ConfirmarCarrito extends HttpServlet {
    
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        String url = "/micrositios/" + (String) request.getAttribute("nomMicro");
        HttpSession session = request.getSession(true);
        String telefono = "";
        String alias = "";
        String horario ="";
        if (session.getAttribute("cliCodigo")!=null){
            if(request.getParameter("cliTelefono")!=null && request.getParameter("cliAlias")!=null){
                telefono = String.valueOf(request.getParameter("cliTelefono"));
                alias = String.valueOf(request.getParameter("cliAlias"));
                if (request.getParameter("cliHorario")!=null){
                    horario = String.valueOf(request.getParameter("cliHorario"));
                }
                String cliente = String.valueOf(session.getAttribute("cliCodigo"));
                String nombreCliente = String.valueOf(session.getAttribute("cliNombre"));
                String campania = String.valueOf(request.getAttribute("codCampania"));
                ArrayList<HashMap> carrito = new ArrayList<HashMap>();
                carrito = (ArrayList<HashMap>) session.getAttribute("carritosesion");

                try{
                    String comNumero = GestorCarrito.confirmarCarrito(campania, cliente, nombreCliente, 
                            alias,horario,telefono, carrito);
                    if(request.getParameter("imprimir")!=null){
                        response.sendRedirect("index?accion=imprimirPresupuesto&opcion=imprimir&comprobante=" + comNumero);
                    }else{
                        carrito.clear();//Limpio el arrayList que se encuentra como variable session
                        session.setAttribute("carritosesion",carrito);//Asigno el arrayList limpio a la session
                        response.sendRedirect("index?accion=principal&_msg_advertencia=Se ha confirmado el carrito");//voy al principal
                    }
                }catch (Exception e) {
                    request.setAttribute("error", e.getMessage());
                }//fin catch
            }else{
                // Especificamos que pagina se coloca en cada parte del layout
                request.setAttribute("1", url + "/encabezado.jsp");
                request.setAttribute("2", url + "/destacado.jsp");
                request.setAttribute("3", url + "/buscador.jsp");
                request.setAttribute("4", "/vistas/pagina/menuizq.jsp");
                request.setAttribute("5", url + "/bannerizq.jsp");

                request.setAttribute("6", "/vistas/pagina/confirmarCarro.jsp");

                request.setAttribute("7", "/vistas/pagina/visitados.jsp");
                request.setAttribute("8", "/vistas/pagina/login.jsp");
                request.setAttribute("9", "/vistas/pagina/rss.jsp");
                request.setAttribute("10", url + "/pie.jsp");
                request.setAttribute("11", "/vistas/pagina/marcas.jsp");    
                dispatch(request, response, "/compositor/pagina/nuevecuerpos.jsp");
                
            }
            
        }

    }    

    protected void doGet(HttpServletRequest request, 
                      HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }

    protected void doPost(HttpServletRequest request, 
                       HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * dispatcher method
     */
    protected void dispatch(HttpServletRequest request,
            HttpServletResponse response, String page) 
            throws javax.servlet.ServletException, 
            java.io.IOException {
        RequestDispatcher dispatcher = 
            getServletContext(). 
            getRequestDispatcher(page);
        dispatcher.forward(request, response);
    }
    

}
